package com.itzg.vboxfarm.config;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.h2.jdbcx.JdbcConnectionPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DataConfig {

	@Configuration
	@Profile("embedded")
	static class Embedded {

		@Bean(destroyMethod="shutdown")
		public DataSource dataSource() {
			EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
			factory.setDatabaseName("vboxfarm");
			factory.setDatabaseType(EmbeddedDatabaseType.H2);
			return factory.getDatabase();		
		}

	}
	
	/**
	 * Standard Data configuration.
	 * @author Keith Donald
	 */
	@Configuration
	@Profile("standard")
	static class Standard {

		@Inject
		private Environment environment;

		@Bean(destroyMethod="dispose")
		public DataSource dataSource() {
			JdbcConnectionPool dataSource = JdbcConnectionPool.create(environment.getProperty("database.url"),
					environment.getProperty("database.username"), environment.getProperty("database.password"));
			return dataSource;
		}
		
	}
}
